<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:48
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
use think\View;

class Base extends Controller
{
    /**
     * 初始化监听登录状态
     */
    public function _initialize()
    {
        if(!session('?user_id')){
            $this->redirect(url('shop/login/indexLogin'));
        }
        //公共数据
        $info = Db::name('u_user')->where('user_id', session('user_id'))->find();
        $info['logo'] = Db::name('about')->where('id', 5)->value('content');
        $info['progress'] = round(($info['user_ram_use']/$info['user_ram'])*100,1);
        $info['user_ram_use'] = round($info['user_ram_use']/1024,1);
        $info['user_ram'] = round($info['user_ram']/1024,1);
        $view = new View();
        $view::share("info",$info);
    }

    public function search($search, $page, $fl = 'search')
    {
        if ($search === null && $page === null) {
            session($fl, null);
        } elseif ($search === '') {
            session($fl, null);
        } elseif ($search !== null) {
            session($fl, serialize($search));
        }
        return unserialize(session($fl));
    }
    public function getUserInfo($user_id,$field='',$type=1)
    {
        if($field){
            if($type == 1){
                $res = Db::table('up_u_user')->where('user_id',$user_id)->value($field);
            }else{
                $res = Db::table('up_u_user')->where('user_id',$user_id)->field($field)->find();
            }
        }else{
            $res = Db::table('up_u_user')->where('user_id',$user_id)->find();
        }
        return $res;

    }
}